package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "巡机结果表")
@Controller
public class WebInspectionrecordController {

    @Autowired
    WebInspectionrecordService webInspectionrecordService;
    @Autowired
    WebInspectionstaffrecordService webInspectionstaffrecordService;

    @Autowired
    HttpServletResponse response;



    @ApiOperation(value = "根据时间查询巡机记录")
    @RequestMapping(value = "/getInspectionrecords",
                    method = { RequestMethod.GET },
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebInspectionrecords(
            HttpServletRequest request,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "startTime", required = false) long startTime,
            @RequestParam(value = "endTime", required = false) long endTime,
            @RequestParam(value = "equipmenttypekey", required = false) String equipmenttypekey,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {
        WebInspectionrecord item = new WebInspectionrecord();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String startsTime = simpleDateFormat.format(new Date(startTime));
        String endsTime = simpleDateFormat.format(new Date(endTime));
        List<WebInspectionrecord> list =  webInspectionrecordService.getListByTime(pageNum, pageSize,startsTime,endsTime,equipmenttypekey,equipmentkey);
        ArrayList<Object> objects = new ArrayList<>();
        list.stream().forEach(ss->{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",ss.getId());
            jsonObject.put("recordnote",ss.getRecordnote());
            jsonObject.put("recordtime",ss.getRecordtime());
            List<WebInspectionstaffrecord> byrecordid = webInspectionstaffrecordService.getByrecordid(ss.getId());
            if (null!=byrecordid){
                String userName ="";
                if(byrecordid.size()>1){
                    for (int i=0;i<byrecordid.size();i++){
                        userName+=byrecordid.get(i).getStaffname()+" ";
                    }
                }else {
                    userName=byrecordid.get(0).getStaffname();
                }
                jsonObject.put("userName",userName);
            }
            objects.add(jsonObject);
        });
        PageInfo result = new PageInfo(objects);
        return new ResponseEntity<>(ResultModel.ok(result), HttpStatus.OK);

    }
}
